import { ref } from "vue";
import { userEnumValue } from "@/api/index.js";

const d1 = ref([]);
const d2 = ref([]);

export const getEnumData = () => {
  userEnumValue().then((res) => {
    for (let i in res.data.region) {
      d1.value.push({ label: res.data.region[i], value: i });
    }
    d2.value = res.data.allowedAgent.map((item) => {
      return { label: item, value: item };
    });
  });
};

export const form = [
  {
    label: "用户名",
    field: "name",
    component: "Input",
    span: 6,
    componentProps: {
      maxlength: 50,
      placeholder: "请输入用户名",
      style: { width: "95%" },
    },
  },
  {
    label: "手机号",
    field: "phone",
    component: "Input",
    span: 6,
    componentProps: {
      maxlength: 50,
      placeholder: "请输入手机号",
      style: { width: "95%" },
    },
  },
];

export const columns = [
  {
    title: "用户名",
    dataIndex: "name",
    ellipsis: true,
    align: "center",
    customRender: ({ record }) => {
      return record.name;
    },
  },
  {
    title: "手机号",
    dataIndex: "phone",
    ellipsis: true,
    align: "center",
    customRender: ({ record }) => {
      return record.phone;
    },
  },
  {
    title: "地市编码",
    dataIndex: "region",
    ellipsis: true,
    align: "center",
    customRender: ({ record }) => {
      return d1.value.find((item) => item.value === record.region)?.label;
    },
  },
  {
    title: "部门名称",
    dataIndex: "department",
    width: 150,
    ellipsis: true,
    align: "center",
    customRender: ({ record }) => {
      return record.department;
    },
  },
  {
    title: "权限生效时间",
    dataIndex: "startTime",
    ellipsis: true,
    align: "center",
    customRender: ({ record }) => {
      return record.startTime;
    },
  },
  {
    title: "权限失效时间",
    dataIndex: "endTime",
    ellipsis: true,
    align: "center",
    customRender: ({ record }) => {
      return record.endTime;
    },
  },
  {
    title: "允许访问的智能体列表",
    dataIndex: "allowedAgent",
    ellipsis: true,
    align: "center",
    customRender: ({ record }) => {
      return record.allowedAgent || "无限制";
    },
  },
  {
    title: "操作",
    dataIndex: "action",
    align: "center",
    fixed: "right",
    width: 250,
  },
];

export const config = ref([
  {
    label: "用户名",
    field: "name",
    component: "Input",
    span: 12,
    componentProps: {
      maxlength: 50,
      placeholder: "请输入用户名",
      style: { width: "95%" },
    },
  },
  {
    label: "手机号",
    field: "phone",
    component: "Input",
    span: 12,
    componentProps: {
      maxlength: 50,
      placeholder: "请输入手机号",
      style: { width: "95%" },
    },
  },
  {
    label: "部门名称",
    field: "department",
    component: "Input",
    span: 12,
    componentProps: {
      maxlength: 50,
      placeholder: "请输入部门名称",
      style: { width: "95%" },
    },
  },

  {
    label: "权限生效时间",
    field: "startTime",
    component: "DatePicker",
    span: 12,
    componentProps: {
      maxlength: 50,
      placeholder: "请选择权限生效时间",
      style: { width: "95%" },
      type: "datetime",
      format: "YYYY-MM-DDHH:mm:ss", // 正确的显示格式
      valueFormat: "YYYY-MM-DD HH:mm:ss", // 正确的绑定值格式
    },
  },
  {
    label: "权限失效时间",
    field: "endTime",
    component: "DatePicker",
    span: 12,
    componentProps: {
      maxlength: 50,
      placeholder: "请选择权限失效时间",
      style: { width: "95%" },
      type: "datetime",
      format: "YYYY-MM-DD HH:mm:ss", // 正确的显示格式
      valueFormat: "YYYY-MM-DD HH:mm:ss", // 正确的绑定值格式
    },
  },
  {
    label: "地区",
    field: "region",
    component: "Select",
    span: 12,
    componentProps: {
      maxlength: 50,
      placeholder: "请选择地区",
      style: { width: "95%" },
      options: d1,
    },
  },
  {
    label: "允许访问的智能体列表",
    field: "allowedAgent",
    component: "Select",
    span: 12,
    componentProps: {
      maxlength: 50,
      placeholder: "请选择允许访问的智能体列表",
      style: { width: "95%" },
      options: d2,
      multiple: true,
    },
  },
]);
export const rules = {
  name: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 2, max: 50, message: "长度在 2 到 50 个字符", trigger: "blur" },
  ],
  phone: [
    { required: true, message: "请输入手机号", trigger: "blur" },
    { min: 11, max: 11, message: "请输入正确的手机号", trigger: "blur" },
  ],
  region: [
    { required: true, message: "请选择地区编码", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  startTime: [
    { required: true, message: "请选择权限生效时间", trigger: "blur" },
  ],
  endTime: [{ required: true, message: "请选择权限失效时间", trigger: "blur" }],
  department: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
  allowedAgent: [
    { required: true, message: "请选择允许访问的智能体列表", trigger: "blur" },
  ],
};
